package com.css.devops.monitor;

import java.net.URI;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.BuildWithDetails;
import com.offbytwo.jenkins.model.JobWithDetails;

public class JenkinsMonitor {

    public static void main(String[] args) {
        try {
            // Jenkins 服务器的 URL 和认证
            URI jenkinsServerUri = new URI("http://192.168.94.1:18080");
            String username = "admin";
            String password = "p@ssw0rd";

            try (// 创建 JenkinsServer 实例
			JenkinsServer jenkins = new JenkinsServer(jenkinsServerUri, username, password)) {
				// 获取 Job
				String jobName = "fresco-web";
				JobWithDetails job = jenkins.getJob(jobName);

				// 获取最新的构建
				Build lastBuild = job.getLastBuild();
				if (lastBuild != null) {
				    // 获取构建详细信息
				    BuildWithDetails buildDetails = lastBuild.details();

				    // 打印构建的 stage 状态
				    System.out.println("Build number: " + buildDetails.getNumber());
				    System.out.println("Build status: " + buildDetails.getResult());

				    // 获取构建阶段
				    // Jenkins Client 不直接支持获取 stages，可能需要使用 Jenkins API 直接调用
				    // 这里是一个简单的例子，具体获取 stage 状态的实现可能需要调用 Jenkins 的 REST API
				} else {
				    System.out.println("No builds found for job: " + jobName);
				}
			}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

